package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"guliedu/middleware"
	"guliedu/model"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type returnComment struct {
	ID          int       `json:"id"`
	CourseName  string    `json:"coursename"`
	TeacherName string    `json:"teachername"`
	MemberName  string    `json:"member_name"`
	Content     string    `json:"content"`
	CreateTime  time.Time `json:"create_time"`
}

type commentInfo struct {
	TeacherID int    `json:"teacherId"`
	CourseID  string `json:"courseId"`
	Content   string `json:"content"`
}

func DeleteComment(ctx *gin.Context) {
	id := ctx.Param("id")
	eduCommentService.Delete(id)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func SelectAll(ctx *gin.Context) {
	page := ctx.Param("page")
	pageSize := ctx.Param("pageSize")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	comments, _ := eduCommentService.SelectAll(p, ps)
	dataSlice := make([]returnComment, 0)
	for _, v := range comments {
		cid := strconv.Itoa(v.CourseID)
		tid := strconv.Itoa(v.TeacherID)
		course, _ := eduCourseService.Select(cid)
		teacher, _ := eduTeacherService.SelectByID(tid)
		temp := returnComment{
			ID:          v.ID,
			CourseName:  course.Title,
			TeacherName: teacher.Name,
			MemberName:  v.Nickname,
			Content:     v.Content,
			CreateTime:  v.CreatedAt,
		}
		dataSlice = append(dataSlice, temp)
	}
	count, _ := eduCommentService.SelectAllCount()
	ctx.JSON(http.StatusOK, gin.H{
		"code":  20000,
		"total": count,
		"row":   dataSlice,
	})
	return
}

func SelectComment(ctx *gin.Context) {
	page := ctx.Param("page")
	pageSize := ctx.Param("pageSize")
	courseID := ctx.Query("courseId")
	ps, _ := strconv.Atoi(pageSize)
	p, _ := strconv.Atoi(page)

	eduComments, _ := eduCommentService.SelectAllByCourseID(courseID)
	count := len(eduComments)
	// 判断是否有前一页
	hasPrevious := false
	if page != "1" {
		hasPrevious = true
	}

	// 判断是否有后一页
	hasNext := true
	t := count / ps
	if p-1 == t {
		hasNext = false
	}

	// 计算所有的页数
	pages := make([]int, 0)
	for i := 0; i <= int(t); i++ {
		pages = append(pages, i+1)
	}

	comments, _ := eduCommentService.SelectByCourseID(courseID, p, ps)
	dataMap := make(map[string]interface{})
	dataMap["hasPrevious"] = hasPrevious
	dataMap["current"] = p
	dataMap["hasNext"] = hasNext
	dataMap["pages"] = pages
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": comments,
		"map":  dataMap,
	})
	return
}

func AddComment(ctx *gin.Context) {
	comment := &commentInfo{}
	ctx.ShouldBind(&comment)

	// 获取当前用户信息
	tokenHeader := ctx.Request.Header.Get("Authorization")
	fmt.Println(tokenHeader)
	if tokenHeader == "" {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 20000,
		})
		return
	}

	// 获取用户信息
	token := strings.Split(tokenHeader, " ")[1]
	authToken, _ := middleware.AuthToken(token)
	member, _ := ucenterMemberService.Select(authToken.UserName)

	// 插入评论数据
	cid, _ := strconv.Atoi(comment.CourseID)
	c := &model.EduComment{
		CourseID:  cid,
		TeacherID: comment.TeacherID,
		MemberID:  member.ID,
		Nickname:  member.Nickname,
		Avatar:    member.Avatar,
		Content:   comment.Content,
	}
	eduCommentService.Insert(c)
	// 更新评论数
	course, _ := eduCourseService.Select(comment.CourseID)
	course.ViewCount++
	eduCourseService.Update(course)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    20000,
		"success": true,
	})
	return
}
